package pk.campustour;

import java.util.List;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

public class CompassActivity extends Activity {
	private static SensorManager mySensorManager;
	private boolean sersorrunning;
	private CompassView myCompassView;

	 /** Called when the activity is first created. */
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	     super.onCreate(savedInstanceState);
	     setContentView(R.layout.activity_compassview);
	  
	     myCompassView = (CompassView)findViewById(R.id.compassview);
	  
	     mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
	     List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
	  
	     if(mySensors.size() > 0){
	      mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
	      sersorrunning = true;
	      Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
	    
	     }
	     else{
	      Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
	      sersorrunning = false;
	      finish();
	     }
	 }

	 private SensorEventListener mySensorEventListener = new SensorEventListener(){

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	 // TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
	 // TODO Auto-generated method stub
	 myCompassView.updateDirection((float)event.values[0]);
	}
	 };

	@Override
	protected void onDestroy() {
	// TODO Auto-generated method stub
	super.onDestroy();

	if(sersorrunning){
	 mySensorManager.unregisterListener(mySensorEventListener);
	}
	}

	}